#!/bin/bash
# This script is released under the GNU General Public License 3.0
# Check the COPYING file included with this distribution

# configure_system
# gives user choices to further edit target system
#
# parameters (required)
#  CONFIG_LIST: One string items of defined FSspec
#
# returns 0 on success
# returns $ERROR_CANCEL=64 on user cancel
# anything else is a real error
# reason: show_dialog() needs a way to exit "Cancel"
#
# writes menus and noise to STDERR

# location of other scripts to source
readonly SHAREDIR="$(dirname ${0})" || exit $?

# source common variables, functions and error handling
source "${SHAREDIR}"/common.sh || exit $?

#########################################
## START: dialog functions/definitions ##

# setpassword()
# sets password for a user
# chroot must be prepared outside this function!
#
# parameters (required)
#  _USERNAME: Name of the user
#
# returns $ERROR_CANCEL=64 on user cancel
# anything else is a real error
# reason: show_dialog() needs a way to exit "Cancel"
#
setpassword(){
	# check input
	check_num_args "${FUNCNAME}" 1 $# || return $?
	local _USERNAME="${1}"
	local _PASSWORD=
	local _RET_SUB=
	_PASSWORD="$(show_dialog --clear --insecure --passwordbox "Enter a password for user '${_USERNAME}'" 17 70)" || return $?
	chroot "${DESTDIR}" /bin/bash <<EOF
echo -e "${_PASSWORD}\n${_PASSWORD}" | passwd "${_USERNAME}" || exit $?
EOF
	_RET_SUB=$?
	_PASSWORD=
	[ "${_RET_SUB}" -ne 0 ] && return "${_RET_SUB}"
	return 0
}

## END: dialog functions/definitions ##
#######################################

#####################
## begin execution ##

# check input
check_num_args "$(basename $0)" 1 $# || exit $?
CONFIG_LIST="${1}"
EDITOR=
MENU_ITEMS=
NEWSELECTION=
USERNAME=
RETSUB=

# mount everything, including cryptsetup
"${SHAREDIR}"/FSspec mountall "${CONFIG_LIST}" || exit $?

chroot_mount || exit $?

# set system editor (if not already defined)
EDITOR="$(geteditor)" || exit $?

# define menu items
MENU_ITEMS=("Editor"					"System Editor" \
			"/etc/conf.d/keymaps"		"Keymap" \
			"/etc/fstab"				"Filesystem Mountpoints" \
			"/etc/resolv.conf"			"DNS Servers" \
			"/etc/hosts"				"Network Hosts" \
			"/etc/locale.gen"			"Glibc Locales" \
			"Root-Password"				"Set the root password" \
			"add-user"					"Add a new user" \
			"DONE"						"Return to Main Menu" )

while true; do
	# expand menu items array below
	NEWSELECTION="$(show_dialog --menu "Configuration" \
		0 0 0 "${MENU_ITEMS[@]}")" || exit $?
	# call subscript by selected item
	case "${NEWSELECTION}" in
		# Return to main menu
		"DONE")
			break ;;
		# System Editor
		"Editor")
			EDITOR="$(seteditor)"
			# capture sub script exit
			RETSUB=$?
			;;
		# root password
		"Root-Password")
			setpassword 'root'
			# capture sub script exit
			RETSUB=$?
			;;
		# add user
		"add-user")
			# 1. ask for user name
			# 2. set up user
			# 3. set password for user
			# 4. copy root profile
			# run rsync with nice dialog
			USERNAME="$(show_dialog --inputbox "Enter a username" 17 70)" \
				&& chroot ${DESTDIR} useradd -m -G users,wheel,audio,cdrom,video,cdrw,usb,plugdev,wireshark,kismet,android ${USERNAME} \
				&& setpassword "${USERNAME}" \
				&& show_dialog_rsync '-r --progress --exclude=.svn --exclude=.subversion' \
					"${DESTDIR}"'/root/.[!.]*' \
					"${DESTDIR}/home/${USERNAME}/" \
					"Syncing ${DESTDIR}/home/${USERNAME}/ ..." \
				&& chroot "${DESTDIR}" chown -R "${USERNAME}":"${USERNAME}" "/home/${USERNAME}" >>"${LOG}"
			RETSUB=$?
			;;
		# locales
		"/etc/locale.gen")
			chroot "${DESTDIR}" "${EDITOR}" "${NEWSELECTION}" \
				&& chroot "${DESTDIR}" locale-gen 1>&2
			RETSUB=$?
			;;
		#regular file
		*)
			chroot "${DESTDIR}" "${EDITOR}" "${NEWSELECTION}"
			RETSUB=$?
			;;
	esac
	sync || exit $?
	# handle errors from sub-script/functions using a common utility function
	catch_menuerror "$(basename $0)" "${NEWSELECTION}" "${RETSUB}"
done

chroot_umount || exit $?

exit 0
